﻿
<template>
<div class="card-fill layout-padding">
<el-card shadow="hover" class="layout-padding-auto" >
  <div>
    <el-form ref="formRefEquipmentAlarmItem" :model="stateEquipmentAlarmItem.vmModel" label-width="100px">
      <el-row>
        <el-col :xs="24" :lg="24" class="mb20">


    <el-form-item ref="Entity_EquipmentId_FormItem" prop="Entity.EquipmentId" :label="$t('message.autotrans._Model_EquipAlarmRecord_Equipment')">
            <el-select v-model="stateEquipmentAlarmItem.vmModel.Entity.EquipmentId" clearable @change="onEquipmentChange">
               <el-option v-for="item in stateEquipmentAlarmItem.AllEquipments" :key="item.Value" :value="item.Value" :label="item.Text"></el-option>
            </el-select>
        </el-form-item>
<!-- 
        <el-form-item ref="Entity_EquipName_FormItem" prop="Entity.EquipName" :label="$t('message.autotrans._Model_EquipmentAlarmItem_EquipName')">
            <el-input v-model="stateEquipmentAlarmItem.vmModel.Entity.EquipName" clearable></el-input>
        </el-form-item>
 -->
        </el-col>
        <el-col :xs="24" :lg="24" class="mb20">
      
        <el-form-item ref="Entity_EquipTagId_FormItem" prop="Entity.EquipTagId" :label="$t('message.autotrans._Model_EquipmentAlarmItem_EquipTag')">
            <el-select v-model="stateEquipmentAlarmItem.vmModel.Entity.EquipTagId"   @change="onEquipmentTagChange" clearable>
               <el-option v-for="item in stateEquipmentAlarmItem.AllEquipTags" :key="item.Value" :value="item.Value" :label="item.Text"></el-option>
            </el-select>
        </el-form-item>

        </el-col>
        <!--  
        <el-col :xs="24" :lg="12" class="mb20">
        <el-form-item ref="Entity_EquipTagName_FormItem" prop="Entity.EquipTagName" :label="$t('message.autotrans._Model_EquipmentAlarmItem_EquipTagName')"            :rules="[{required: true, message: $t('message._system.common.vm.input',{input:$t('message.autotrans._Model_EquipmentAlarmItem_EquipTagName')}), trigger: 'blur' }]" >
            <el-input v-model="stateEquipmentAlarmItem.vmModel.Entity.EquipTagName" clearable></el-input>
        </el-form-item>
        </el-col>

        <el-col :xs="24" :lg="12" class="mb20">
        <el-form-item ref="Entity_EquipTagAddress_FormItem" prop="Entity.EquipTagAddress" :label="$t('message.autotrans._Model_EquipmentAlarmItem_EquipTagAddress')"            :rules="[{required: true, message: $t('message._system.common.vm.input',{input:$t('message.autotrans._Model_EquipmentAlarmItem_EquipTagAddress')}), trigger: 'blur' }]" >
            <el-input v-model="stateEquipmentAlarmItem.vmModel.Entity.EquipTagAddress" clearable></el-input>
        </el-form-item>
       
        </el-col>
         -->
        <el-col :xs="24" :lg="24" class="mb20">
        <el-form-item ref="Entity_Content_FormItem" prop="Entity.Content" :label="$t('message.autotrans._Model_EquipmentAlarmItem_Content')"            :rules="[{required: true, message: $t('message._system.common.vm.input',{input:$t('message.autotrans._Model_EquipmentAlarmItem_Content')}), trigger: 'blur' }]" >
            <el-input v-model="stateEquipmentAlarmItem.vmModel.Entity.Content" clearable></el-input>
        </el-form-item>

        </el-col>
        <el-col :xs="24" :lg="24" class="mb20">
        <el-form-item ref="Entity_Level_FormItem" prop="Entity.Level" :label="$t('message.autotrans._Model_EquipmentAlarmItem_Level')"            :rules="[{required: true, message: $t('message._system.common.vm.input',{input:$t('message.autotrans._Model_EquipmentAlarmItem_Level')}), trigger: 'blur' }]" >
            <el-select v-model="stateEquipmentAlarmItem.vmModel.Entity.Level" clearable>
               <el-option key="L1" value="L1" :label="$t('message.autotrans._Enum_AlarmLevelEnum_L1')"></el-option>
               <el-option key="L2" value="L2" :label="$t('message.autotrans._Enum_AlarmLevelEnum_L2')"></el-option>
               <el-option key="L3" value="L3" :label="$t('message.autotrans._Enum_AlarmLevelEnum_L3')"></el-option>
               <el-option key="L4" value="L4" :label="$t('message.autotrans._Enum_AlarmLevelEnum_L4')"></el-option>
               <el-option key="L5" value="L5" :label="$t('message.autotrans._Enum_AlarmLevelEnum_L5')"></el-option>
            </el-select>
        </el-form-item>

        </el-col>
        <el-col :xs="24" :lg="24" class="mb20">
        <el-form-item ref="Entity_Classify_FormItem" prop="Entity.Classify" :label="$t('message.autotrans._Model_EquipmentAlarmItem_Classify')"            :rules="[{required: true, message: $t('message._system.common.vm.input',{input:$t('message.autotrans._Model_EquipmentAlarmItem_Classify')}), trigger: 'blur' }]" >
            <el-select v-model="stateEquipmentAlarmItem.vmModel.Entity.Classify" clearable>
               <el-option key="Robot" value="Robot" :label="$t('message.autotrans._Enum_AlarmTypeEnum_Robot')"></el-option>
               <el-option key="EStop" value="EStop" :label="$t('message.autotrans._Enum_AlarmTypeEnum_EStop')"></el-option>
               <el-option key="SensorOver" value="SensorOver" :label="$t('message.autotrans._Enum_AlarmTypeEnum_SensorOver')"></el-option>
               <el-option key="Cylinder" value="Cylinder" :label="$t('message.autotrans._Enum_AlarmTypeEnum_Cylinder')"></el-option>
               <el-option key="Raster" value="Raster" :label="$t('message.autotrans._Enum_AlarmTypeEnum_Raster')"></el-option>
               <el-option key="Welder" value="Welder" :label="$t('message.autotrans._Enum_AlarmTypeEnum_Welder')"></el-option>
               <el-option key="Servo" value="Servo" :label="$t('message.autotrans._Enum_AlarmTypeEnum_Servo')"></el-option>
               <el-option key="Welding" value="Welding" :label="$t('message.autotrans._Enum_AlarmTypeEnum_Welding')"></el-option>
               <el-option key="Vision" value="Vision" :label="$t('message.autotrans._Enum_AlarmTypeEnum_Vision')"></el-option>
               <el-option key="Airtight" value="Airtight" :label="$t('message.autotrans._Enum_AlarmTypeEnum_Airtight')"></el-option>
               <el-option key="General" value="General" :label="$t('message.autotrans._Enum_AlarmTypeEnum_General')"></el-option>
            </el-select>
        </el-form-item>

        </el-col>
        <el-col :xs="24" :lg="24" class="mb20">
        <el-form-item ref="Entity_Bits_FormItem" prop="Entity.Bits" :label="$t('message.autotrans._Model_EquipmentAlarmItem_Bits')"            :rules="[{required: true, message: $t('message._system.common.vm.input',{input:$t('message.autotrans._Model_EquipmentAlarmItem_Bits')}), trigger: 'blur' }]" >
           <el-input-number v-model="stateEquipmentAlarmItem.vmModel.Entity.Bits" clearable />
        </el-form-item>

        </el-col>
        <el-col :xs="24" :lg="24" class="mb20">
        <el-form-item ref="Entity_Active_FormItem" prop="Entity.Active" :label="$t('message.autotrans._Model_EquipmentAlarmItem_Active')"            :rules="[{required: true, message: $t('message._system.common.vm.input',{input:$t('message.autotrans._Model_EquipmentAlarmItem_Active')}), trigger: 'blur' }]" >
            <el-switch v-model="stateEquipmentAlarmItem.vmModel.Entity.Active" clearable></el-switch>
        </el-form-item>

        </el-col>
      </el-row>

      <div style="text-align:right;">
        <WtmButton @click="onSubmitEquipmentAlarmItem"  type="primary" :button-text="$t('message.autotrans.SysSubmit')" style="margin-top:15px;"/>

        <WtmButton @click="onCloseEquipmentAlarmItem"  type="primary" :button-text="$t('message.autotrans.SysClose')" style="margin-top:15px;"/>

      </div>

    </el-form>

  </div>

</el-card>
</div>
</template>


<script setup lang="ts" name="message.autotrans._PageEquipmentManagementEquipmentAlarmItemCreate;false">
import {  ElMessageBox, ElMessage,ElLoading } from 'element-plus';
import { defineAsyncComponent,reactive, ref, getCurrentInstance, onMounted, nextTick } from 'vue';
import { equipmentalarmitemApi } from '/@/api/equipmentmanagement/equipmentalarmitem';
import other from '/@/utils/other';
import fileApi from '/@/api/file';
import { useRouter } from "vue-router";
const ci = getCurrentInstance() as any;


// 定义变量内容
const formRefEquipmentAlarmItem = ref();
const stateEquipmentAlarmItem = reactive({
    vmModel: {
	  Entity:{
        EquipName: '',
        EquipmentId:null,
        EquipTagId: null,
        EquipTagName: '',
        EquipTagAddress: '',
        Content: '',
        Level: undefined,
        Classify: undefined,
        Bits: 1,
        Active: true,
      },

	},
     AllEquipments: [] as any[],
    AllEquipTags: [] as any[],
});

// 取消
const onCloseEquipmentAlarmItem = () => {
    closeDialog();
};

// 提交
const onSubmitEquipmentAlarmItem = () => {
 	const loadingInstance = ElLoading.service({
		lock: true,
		text: '正在提交中，请等待。。。',
		background: 'rgba(0, 0, 0, 0.7)',
	});
   formRefEquipmentAlarmItem.value?.validate((valid: boolean, fields: any) => {
		if (valid) {
            equipmentalarmitemApi().add(stateEquipmentAlarmItem.vmModel).then(() => {
                ElMessage.success(ci.proxy.$t('message._system.common.vm.submittip'));
                emit('refresh');
                closeDialog();
            }).catch((error) => {
                other.setFormError(ci, error);
            }).finally(() => {
			loadingInstance.close();
		    })
		}
        else{
			loadingInstance.close();
        }
	})
};


//选择设备时
const onEquipmentChange = () => {
  const equipmentId = stateEquipmentAlarmItem.vmModel.Entity.EquipmentId;

  if (equipmentId) {
    // 父级有选中值：加载该设备对应的标签列表
    other.getSelectList(
      `/api/EquipmentManagement/EquipmentAlarmItem/GetByEquipmentTags?id=${equipmentId}`,
      [],
      false
    ).then((tags) => {
      // 更新子级选项：仅显示当前设备的标签
      stateEquipmentAlarmItem.AllEquipTags = tags;
    });
   //获取设备名称
   const selectequip=stateEquipmentAlarmItem.AllEquipments.find((item)=>item.Value=== equipmentId);
   if (selectequip)
   {
       stateEquipmentAlarmItem.vmModel.Entity.EquipName=selectequip.Text;
   }
   else{
         stateEquipmentAlarmItem.vmModel.Entity.EquipName='';
   }



  } else {
    // 父级未选中或被清空：重置子级状态
    stateEquipmentAlarmItem.AllEquipTags = []; // 清空标签列表
    stateEquipmentAlarmItem.vmModel.Entity.EquipName='';
    stateEquipmentAlarmItem.vmModel.Entity.EquipTagId = null; // 清空标签选中值
  }
};

//选择地址时
const onEquipmentTagChange = (val: any) => {
  if (val) {
    // 从AllEquipTags中找到Value等于val的标签对象
    const selectedTag = stateEquipmentAlarmItem.AllEquipTags.find(
      (item) => item.Value === val
    );
    // 如果找到，将其Address赋值给EquipTagAddress
    if (selectedTag) {
      stateEquipmentAlarmItem.vmModel.Entity.EquipTagAddress = selectedTag.Text;
    } else {
      stateEquipmentAlarmItem.vmModel.Entity.EquipTagAddress = '';
    }
  } else {
    stateEquipmentAlarmItem.vmModel.Entity.EquipTagAddress = '';
  }
};

// 页面加载时
onMounted(() => {
    
    other.getSelectList('/api/EquipmentManagement/EquipAlarmRecord/GetEquipments',[],false).then(x=>{stateEquipmentAlarmItem.AllEquipments = x});
    
});

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh','closeDialog']);
// 关闭弹窗
const closeDialog = () => {
    emit('closeDialog');
};
// 暴露变量
defineExpose({

});
</script>

<style scoped lang="scss">

</style>

